﻿using Harmontronics.Api.Config;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Business;
using Harmontronics.Model.Dto.Login;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Dto.User;
using Harmontronics.Model.Entitys;
using Harmontronics.Model.Other;
using Harmontronics.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;

namespace Harmontronics.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    // [Authorize]
    [ApiController]
    public class WorkStationController : ControllerBase
    {
        private IStationService _stationService;

        public WorkStationController(IStationService stationService)
        {
            _stationService = stationService;
        }
        [HttpPost]
        public ApiResponseData GetWorkStationData(StationInput input)
        {
            List<Station> entityList = _stationService.GetAllStations();
            //自身id搜索
            if (!input.WorkStationId.IsNullOrEmpty())
            {
                entityList = entityList.Where(i => i.WorkStationId.Contains(input.WorkStationId)).ToList();
            }
            //ProductionLineId搜索
            if (!input.ProductionLineId.IsNullOrEmpty())
            {
                entityList = entityList.Where(i => i.ProductionLineId.Contains(input.ProductionLineId)).ToList();
            }
            //plc搜索
            if (!input.PlcId.IsNullOrEmpty())
            {
                entityList = entityList.Where(i => i.PlcId.Contains(input.PlcId)).ToList();
            }
            int count = entityList.Count;
            if (!entityList.IsNullOrEmpty())
            {
                int start = (input.page - 1) * input.size;
                int end = ((start + input.size) >= (entityList.Count)) ? (entityList.Count) : (start + input.size);
                entityList = entityList.GetRange(start, end - start);
            }
            List<StationModel> ModelList = _stationService.MendingRange(entityList);
            return ResultHelper.Success(new ReturnData() { total = count, dataList = ModelList });

        }
        [HttpGet]
        public ApiResponseData GetAllWorkStationData()
        {
            List<Station> model = _stationService.GetAllStations();
            return ResultHelper.Success(new ReturnData() { total = model.Count, dataList = model });

        }
        [HttpPost]
        public ApiResponseData AddWorkStation(Station model)
        {
            //判断重复
            if (_stationService.GetStationsById(model.WorkStationId) != null)
            {
                return ResultHelper.Error(502, "ID cannot be repeated! ");
            }
            if (_stationService.AddStation(model))
            {
                return ResultHelper.Success("Add successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to add!");
            }
        }
        [HttpPost]
        public ApiResponseData UpdateWorkStation(Station model)
        {
            if (_stationService.GetStationsById(model.WorkStationId) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + model.WorkStationId + "!");
            }
            if (_stationService.UpdateStation(model))
            {
                return ResultHelper.Success("Update successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Update!");
            }
        }
        [HttpPost]
        public ApiResponseData DeleteWorkStation(DeleteStringId id)
        {
            if (_stationService.GetStationsById(id.id) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + id + "!");
            }
            if (_stationService.DeleteStationById(id.id))
            {
                return ResultHelper.Success("Delete successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Delete!");
            }
        }
        [HttpPost]
        public ApiResponseData BatchDelWorkStation(List<string> batchRemoveDatas)
        {
            if (batchRemoveDatas.Count > 0)
            {
                if (_stationService.BatchDelWorkStation(batchRemoveDatas))
                {
                    return ResultHelper.Success("Delete successfully!");
                }
                else
                {
                    return ResultHelper.Error(500, "Failed to Delete!");
                }
            }
            return ResultHelper.Error(502, "No data detected!");
        }

    }
}
